package com.fantasy.servlet;

import com.fantasy.entity.Admin;
import com.fantasy.entity.Page;
import com.fantasy.service.AdminService;
import com.fantasy.service.impl.AdminServiceImpl;
import com.fantasy.utils.WebUtils;
import com.google.gson.Gson;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/admin.do")
public class AdminServlet extends BaseServlet {
    private AdminService adminService=new AdminServiceImpl();
    /**
     * 查询全部方法
     * @param request
     * @param response
     */
    public void queryAll(HttpServletRequest request,HttpServletResponse response) throws IOException {
        //查询全部admin
        List<Admin> admins = adminService.queryAll();
        //System.out.println(admins);

        //使用gson将list转化为json对象
        Gson gson=new Gson();
        String json = gson.toJson(admins);
        //System.out.println(json);
        
        //向前端发送json对象
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(json);
    }

    /**
     * 登录方法
     * @param request
     * @param response
     */
    public void login(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
        //获取前台数据
        boolean remember = Boolean.parseBoolean(request.getParameter("rememberMe"));
        String phone = request.getParameter("phone");
        String password = request.getParameter("password");
        response.setContentType("text/html,charset=utf-8");
        //查询账号密码是否存在
        Admin admin = adminService.queryByPhoneAndPassword(phone, password);
        if (admin!=null) {
            //登录成功
            if (remember){
                //判断记住我是否点上
                //添加到cookie中
                Cookie cookie = new Cookie("admin", admin.getPhone());
                cookie.setMaxAge(60 * 60 * 24 * 7);
                response.addCookie(cookie);
                //response.getWriter().write(1);
            }else {
                //添加到cookie中
                Cookie cookie = new Cookie("username", null);
                cookie.setMaxAge(60 * 60 * 24 * 7);
                response.addCookie(cookie);
                //response.getWriter().write(0);
            }
            HttpSession session = request.getSession();
            session.setAttribute("admin",admin);
            response.getWriter().write("1");
            //response.sendRedirect("./index.html");
            //System.out.println(session);
        }else {
            //登录失败
            response.getWriter().write("0");
        }

    }

    /**
     * 添加管理员
     * @param request
     * @param response
     */
    public void addAdmin(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        Admin admin=new Admin();
        admin.setName(name);
        admin.setPassword(password);
        admin.setEmail(email);
        admin.setPhone(phone);
        int insert = adminService.insert(admin);
        //System.out.println(insert);
        response.setContentType("text/html;charset=utf-8");
        if (insert==1){
            response.getWriter().write("1");
        }else {
            response.getWriter().write("0");
        }
    }

    /**
     * 修改管理员
     * @param request
     * @param response
     */
    public void updateAdmin(HttpServletRequest request,HttpServletResponse response) throws IOException {

        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        Admin admin=new Admin();
        admin.setId(id);
        admin.setName(name);
        admin.setPassword(password);
        admin.setEmail(email);
        admin.setPhone(phone);
        int update = adminService.update(admin);
        response.setContentType("text/html;charset=utf-8");
        if (update==1){
            response.getWriter().write("1");
        }else {
            response.getWriter().write("0");
        }
    }

    /**
     * 删除admin
     * @param request
     * @param response
     * @throws IOException
     */
    public void deleteAdmin(HttpServletRequest request,HttpServletResponse response) throws IOException {

        int id = Integer.parseInt(request.getParameter("id"));
        int delete = adminService.delete(id);
        response.setContentType("text/html;charset=utf-8");
        if (delete==1){
            response.getWriter().write("1");
        }else {
            response.getWriter().write("0");
        }
    }

    /**
     * 分页查询
     * @param request
     * @param response
     * @throws IOException
     */
    public void pageQuery(HttpServletRequest request,HttpServletResponse response) throws IOException {
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"), 0);
        int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), Page.DEFAULT_PAGE_SIZE);

        Page<Admin> adminPage = adminService.pageQuery(pageNo, pageSize);

        Gson gson=new Gson();
        String adminJson = gson.toJson(adminPage);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(adminJson);
    }

    /**
     * 退出
     * @param request
     * @param response
     * @throws IOException
     */
    public void exit(HttpServletRequest request,HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        session.invalidate();
    }
}
